/**
 *  Eclipse Public License 1.0
 */
package org.fireblade.easysms;

import java.io.BufferedWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Service;
import android.database.Cursor;

/**
 * Action for getting the addressbook html page.
 */
public class HttpActionGetAddressbook {

  /**
   * @param values
   * @param writer
   * @param service the context/service
   * @throws Exception
   */
  public static void process(Service service, Map<String, String> values, BufferedWriter writer)
    throws Exception {
    writer
      .write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\r\nhttp://www.w3.org/TR/html4/strict.dtd>\r\n<html><head>"
      + "<link rel=\"stylesheet\" href=\"/style.css\" type=\"text/css\" media=\"screen\" />"
      + "<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; charset=utf-8\"></head><body  bgcolor=\"#DDDDDD\">");
    Util.writeScript(service, writer, false);
    writer.write("<table width=\"100%\">");
    writeAddressbook(service, writer);
    writer.write("</table></body></html>");
  }


  /**
   * @param writer
   */
  private static void writeAddressbook(Service service, BufferedWriter writer) throws Exception {
    Cursor cursor = null;

    List<Contact> contactsOrdered = new ArrayList<Contact>();
    Map<Integer, Contact> contacts = new HashMap<Integer, Contact>();
    while (cursor.moveToNext()) {
      int contactId = cursor.getInt(0);
      Contact contact = contacts.get(contactId);
      if (null == contact) {
        contact = new Contact();
        contacts.put(contactId, contact);
      }
      PhoneNumber number = new PhoneNumber();
      contact.numbers.add(number);
    }
    cursor.close();

    writer.write("<tr bgcolor=\"#EEEEEE\">");
    writer.write("<td colspan=3 onClick=\"filterAddressbook()\">");
    writer
      .write("    Filter (Name): <form name=\"searchform\"><input type=\"text\" name=\"searchit\" value=\"\" onkeyup=\"filterAddressbook(this.value);\"/></form>");
    writer.write("</td>");
    writer.write("</tr>");
    
    for (Contact c : contactsOrdered) {
      boolean oneNumber = c.numbers.size() == 1;
      String theMainNumber = "onClick=\"addNumber('" + c.displayName + "','" + c.numbers.get(0).number + "')\"";
      if (oneNumber) {
        writer.write("<tr id=\"" + c.displayName + "\" bgcolor=\"#EEEEEE\" " + theMainNumber + " >");
      } else {
        writer.write("<tr id=\"" + c.displayName + "\" bgcolor=\"#EEEEEE\">");
      }
      writer.write("<td width=\"3\"  " + (!c.isStarred ? "bgcolor=\"#EEEEEE\"" : "bgcolor=\"#FFFF33\"") + ">"
        + (c.isStarred ? "<b>T O P</b>" : "") + "</td>");

      writer.write("<td " + (oneNumber ? "" : theMainNumber)
        + " width=48><img width=48 height=48 src=\"/getpicture?id=" + c.contactId + "\"/>");
      writer.write("</td><td valign=top><table width=\"100%\">");
      writer.write("<tr><td " + (oneNumber ? "" : theMainNumber)
        + " width=\"50%\" valign=top rowspan=3><b>"
        + c.displayName + "</b></td><td "
        + (oneNumber ? "" : theMainNumber) + " ><b>" + c.numbers.get(0).number + "</b></td></tr>");
      writer.write("<tr><td "
        + (c.numbers.size() > 1 ? "onClick=\"addNumber('" + c.displayName + "','" + c.numbers.get(1).number
          + "')\" " : "") + ">" + (c.numbers.size() > 1 ? c.numbers.get(1).number : "&nbsp;") + "</td></tr>");
      writer.write("<tr><td "
        + (c.numbers.size() > 2 ? "onClick=\"addNumber('" + c.displayName + "','" + c.numbers.get(2).number
          + "')\" " : "") + ">" + (c.numbers.size() > 2 ? c.numbers.get(2).number : "&nbsp;") + "</td></tr>");
      writer.write("</table></td></tr>");
    }
    cursor.close();
  }
}
